﻿using HuaJiLedger.Models.Ledger;
using HuaJiLedger.Repository;
using HuaJiLedger.Repositorys;
using System.Collections.ObjectModel;

namespace HuaJiLedger.ViewModels.Home;

internal class HomeViewModel : ViewModelBase , IQueryAttributable
{
    private readonly LedgerItemRepository repository;
    private double todayTotalSpend;
    private string dailyWord;
    private bool isRefreshing;
    private double refreshViewHeight;

    public double TodayTotalSpend
    { get => todayTotalSpend; set => SetProperty(ref todayTotalSpend, value); }
    public string DailyWord
    { get => dailyWord; set { SetProperty(ref dailyWord, value); } }
    public bool IsRefreshing
    { get => isRefreshing; set => SetProperty(ref isRefreshing, value); }
    public double RefreshViewHeight
    { get => refreshViewHeight; set => SetProperty(ref refreshViewHeight, value); }

    public ObservableCollection<LedgerItem> Items { get; set; }

    public Command ItemTappedCommand { get; set; }
    public Command AddItemButtonTappedCommand { get; set;}
    public Command RefreshCommand { get; set; }

    public HomeViewModel(Page page)
    {
        this.page = page;

        Items = new ObservableCollection<LedgerItem>();
        repository = new LedgerItemRepository();
        RefreshViewHeight = 0;

        ItemTappedCommand = new Command(async (object obj) => await ActionItemTappedCommandAsync(obj));
        AddItemButtonTappedCommand = new Command(ActionAddItemButtonTappedCommand);
        RefreshCommand = new Command(ActionRefreshCommand);

        SetDailyWord();
        SetItemsAsync();
    }

    void SetDailyWord()
    {
        var word = DailyWordRepository.GetDailyWord();
        DailyWord = $"每日一句：{word.Content}   ---{(string.IsNullOrEmpty(word.Author) ? "匿名" : word.Author)}";
    }

    async void SetItemsAsync()
    {
        Items.Clear();
        RefreshViewHeight = 0;

        (await repository.GetLedgerItemsTodayAsync()).ForEach((item) =>
        {
            Items.Add(item);
            TodayTotalSpend += item.InOut == 0 ? item.Money : 0;
            RefreshViewHeight += 60;
        });
    }

    void ActionRefreshCommand()
    {
        IsRefreshing = true;
        TodayTotalSpend = 0;
        RefreshViewHeight = 0;
        SetItemsAsync();
        IsRefreshing = false;
    }

    async Task ActionItemTappedCommandAsync(object obj)
    {
        var action = await page.DisplayActionSheet("你要对这条记录做什么?", "取消", null, "查看详情", "删除");

        if (string.IsNullOrEmpty(action))
            return;

        var item = obj as LedgerItem;
        switch (action)
        {
            case "查看详情":
                await Shell.Current.GoToAsync($"Home/LedgerItemDetailPage?ID={item.Id}");
                break;
            case "删除":
                Items.Remove(item);
                RefreshViewHeight -= 60;
                TodayTotalSpend -= item.InOut == 0 ? item.Money : 0;
                await repository.DeleteLedgerItemAsync(item);
                break;
            default:
                break;
        }
    }

    async void ActionAddItemButtonTappedCommand()
    {
        await Shell.Current.GoToAsync("Home/AddLedgerItemPage");
    }

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        if (query.Count <= 0)
            return;

        if (query.TryGetValue("isRefresh", out object _isRefresh))
        {
            if ((string)_isRefresh == "1")
                SetItemsAsync();
        }
    }
}
